package com.tb.boxman;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;
import android.view.MotionEvent;

/**
 * Created by 汪贵春 on 2016/7/10.
 */
public class GameMenu {

    private Bitmap  menuBg;
    private Bitmap  buttonStart;
    private Bitmap  buttonStartPressed;
    private Bitmap  buttonQuit;
    private Bitmap  buttonQuitPressed;
    private boolean isPressedStart;
    private boolean isPressedQuit;

    private int x1, x2, y1, y2, y3, y4;

    public GameMenu() {

        Resources res = MainActivity.main.getResources();
        menuBg = BitmapFactory.decodeResource(res, R.drawable.menu_bg);
        buttonStart = BitmapFactory.decodeResource(res, R.drawable.button_start);
        buttonStartPressed = BitmapFactory.decodeResource(res, R.drawable.button_start_pressed);
        buttonQuit = BitmapFactory.decodeResource(res, R.drawable.button_quit);
        buttonQuitPressed = BitmapFactory.decodeResource(res, R.drawable.button_quit_pressed);

    }


    public void draw(Canvas canvas, Paint paint) {
        x1 = GameView.screenW / 5;
        x2 = GameView.screenW - x1;
        y4 = GameView.screenH - x1 / 2;
        y3 = y4 - x1;
        y2 = y3 - x1 / 2;
        y1 = y2 - x1;


        float scale  = Math.max((float) GameView.screenW / menuBg.getWidth(), (float) GameView.screenH / menuBg.getHeight());
        float startX = GameView.screenW / 2 - scale * menuBg.getWidth() / 2;
        float startY = GameView.screenH / 2 - scale * menuBg.getHeight() / 2;


        RectF dst = new RectF(startX, startY, startX + scale * menuBg.getWidth(), startY + scale * menuBg.getHeight());
        canvas.drawBitmap(menuBg, null, dst, paint);

        dst.set(x1, y3, x2, y4);
        if (isPressedQuit) {
            canvas.drawBitmap(buttonQuitPressed, null, dst, paint);
        } else {
            canvas.drawBitmap(buttonQuit, null, dst, paint);
        }

        dst.set(x1, y1, x2, y2);
        if (isPressedStart) {
            canvas.drawBitmap(buttonStartPressed, null, dst, paint);
        } else {
            canvas.drawBitmap(buttonStart, null, dst, paint);
        }

    }


    public void onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if (x > x1 && x < x2) {
            Log.d("tb", x + " + " + y);
            if (y > y1 && y < y2) {
                isPressedStart = true;
            } else {
                isPressedStart = false;
            }

            if (y > y3 && y < y4) {
                isPressedQuit = true;
            } else {
                isPressedQuit = false;
            }
        } else {
            isPressedStart = false;
            isPressedQuit = false;
        }

        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (isPressedStart) {
                GameView.gameState = GameView.GAMING;
                Gaming.roundPass = true;

            }
            if (isPressedQuit) {
                System.exit(0);
            }
            isPressedStart = false;
            isPressedQuit = false;
        }

    }


    public void onBackKeyUp(){
        System.exit(0);
    }

}
